package in.muton.edushare.grading.panels.studentGrades;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.grading.panels.gradeDisplay.UserView;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Users;

public class StudentCourseView extends EdusharePanel {
	private static final long serialVersionUID = -2621476853193199555L;

	public StudentCourseView(String id, EduShareBasePage page,
			final IModel<CourseInstances> cInst,
			final StudentMainGradePanel panel) {
		super(id, page);
		this.panel = panel;
		add(new EduShareURLPanel("courseName", cInst));
		add(new Link<Void>("edit") {
			private static final long serialVersionUID = 2847608356167643785L;

			@Override
			public void onClick() {
				panel.setActiveMode(StudentMainGradePanel.EDIT_COURSE_GRADES);
			}

			@Override
			public boolean isVisible() {
				CourseRoleCheck crc = new CourseRoleCheck(cInst.getObject(),
						getLoggedInUser());
				return crc.isAdmin() || crc.isTeacher();
			}
		});

		CayenneList<Users> studentList = new CourseDataHandler(cInst)
				.getStudents();

		add(new ListView<IModel<Users>>("studentGradesLV", studentList) {
			private static final long serialVersionUID = -5647421782239236102L;

			@Override
			protected void populateItem(final ListItem<IModel<Users>> arg0) {
				boolean isVisible = StudentItemView.isStudentInSearch(arg0
						.getModelObject().getObject(), panel
						.getStudentName());
				arg0.add(new UserView("studentGrades", arg0.getModelObject(),
						cInst));
				arg0.setVisible(isVisible);
			}
		});
	}

	@Override
	public boolean isVisible() {
		return (panel.getActiveMode() == StudentMainGradePanel.VIEW_COURSE_GRADES);
	}

	private StudentMainGradePanel panel = null;
}
